package com.moli.iov.open.web.controller;

import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.open.model.vo.req.RoleVo;
import com.moli.iov.open.service.IRoleFacadeService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.model.dto.req.RolePermissionDto;
import com.moli.iov.sys.model.po.Role;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色业务
 * </p>
 *
 * @author zql
 * @since 2019/9/21 15:48
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

  @Autowired
  private IRoleFacadeService roleFacadeService;

  /**
   *
   * 查询角色列表--下拉框
   *
   * @param roleVo
   * @return
   */
  @RequestMapping("/listRole")
  public RestResponse<List<Role>> listRole(@RequestBody(required = false) RoleVo roleVo){
    if(roleVo == null){
      roleVo = new RoleVo();
    }
    assembleUserInfo(roleVo);
    return roleFacadeService.listRole(roleVo);
  }

  /**
   *
   * 分页查询角色列表
   *
   * @param roleVo
   * @return
   */
  @RequestMapping("/listRoleOfPage")
  public RestResponse<PageExtend<Role>> listRoleOfPage(@RequestBody(required = false) RoleVo roleVo){
    if(roleVo == null){
      roleVo = new RoleVo();
    }
    assembleUserInfo(roleVo);
    log.info("分页查询角色列表 入参：{}", roleVo);
    return roleFacadeService.listRoleOfPage(roleVo);
  }

  /**
   * 增加角色
   * @param roleVo
   * @return
   */
  @RequestMapping("/saveRole")
  public RestResponse saveRole(@RequestBody @Validated({RoleVo.SaveRole.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    return roleFacadeService.saveRole(roleVo);
  }

  /**
   * 修改角色
   * @param roleVo
   * @return
   */
  @RequestMapping("/update")
  public RestResponse updateRoleByRoleId(@RequestBody @Validated({RoleVo.UpdateRoleByRoleId.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    return roleFacadeService.updateRoleByRoleId(roleVo);
  }

  /**
   * 设置权限
   * @param roleVo
   * @return
   */
  @RequestMapping("/setPermission")
  public RestResponse setPermissione(@RequestBody @Validated({RoleVo.SetPermissione.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    return roleFacadeService.setPermission(roleVo);
  }

  /**
   * 权限详情
   * @param roleVo
   * @return
   */
  @RequestMapping("/getRolePermissions")
  public RestResponse<RolePermissionDto> getRolePermissions(@RequestBody @Validated({RoleVo.GetRolePermissions.class}) RoleVo roleVo){
    assembleUserInfo(roleVo);
    return roleFacadeService.getRolePermissions(roleVo);
  }

  /**
   * 查询角色数量
   * @return
   */
  @RequestMapping("/getRoleCount")
  public RestResponse<Integer> getRoleCount(){
    RoleVo roleVo = new RoleVo();
    assembleUserInfo(roleVo);
    return roleFacadeService.getRoleCount(roleVo);
  }
}
